/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.quartz.task;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.quartz.service.QuartzJobService;
import me.zhengjie.suli.inventory.domain.Inventory;
import me.zhengjie.suli.inventory.repository.InventoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Date;
import java.util.List;


/**
 * 测试用
 *
 * @author Zheng Jie
 * @date 2019-01-08
 */
@Slf4j
@Service
public class ScanTasks {
    @Autowired
    private InventoryRepository inventoryRepository;

    @Autowired
    QuartzJobService quartzJobService;

    private static Connection connection;
    private static Statement statement;
    public void time1() {
        try{
            log.info("开始同步库存");
            if (connection == null || statement == null) {
                ScanTasks.getConnection();
            }
            StringBuilder sql = new StringBuilder();
            sql.append("insert into wms(id,location_code,warehouse_code,start_date,create_by,update_by,create_time,update_time,zone_code,area_code," +
                    "warehouse_location,grade,box_number,source_documents,material_code,material_name," +
                    "material_number,lot_code,is_hold,su_no,el_unit,vmi_code,hold_quantity,total,available_quantity,ch_seq,wa_no,traycode,el_mkdate) VALUES");
            List<Inventory> inventoryList = inventoryRepository.findAll();
            if (null != inventoryList && inventoryList.size() > 1) {
                for (int i = 0; i < inventoryList.size(); i++) {
                    sql.append("('" + inventoryList.get(i).getId() + "','" + inventoryList.get(i).getLocationCode() + "','" + inventoryList.get(i).getWarehouseCode() + "','"
                            + inventoryList.get(i).getStartDate() + "','" + inventoryList.get(i).getCreateBy() + "','" + inventoryList.get(i).getUpdateBy() + "'," + "'" + inventoryList.get(i).getCreateTime() + "'" + ",'"
                            + inventoryList.get(i).getUpdateTime() + "','" + inventoryList.get(i).getZoneCode() + "','" + inventoryList.get(i).getAreaCode() + "','" + inventoryList.get(i).getWarehouseLocation() + "','"
                            + inventoryList.get(i).getGrade() + "','" + inventoryList.get(i).getBoxNumber() + "','" + inventoryList.get(i).getSourceDocuments() + "','"
                            + inventoryList.get(i).getMaterialCode() + "','" + inventoryList.get(i).getMaterialName() + "','" + (null != inventoryList.get(i).getMaterialNumber() ? inventoryList.get(i).getMaterialNumber() : new BigDecimal(0)) + "','"
                            + inventoryList.get(i).getLotCode() + "'," + inventoryList.get(i).getIsHold() + ",'" + inventoryList.get(i).getSuNo() + "','" + inventoryList.get(i).getElUnit() + "','"
                            + inventoryList.get(i).getVmiCode() + "'," + inventoryList.get(i).getHoldQuantity() + "," + inventoryList.get(i).getTotal() + "," + inventoryList.get(i).getAvailableQuantity() + ",'"
                            + inventoryList.get(i).getChSeq() + "','" + inventoryList.get(i).getWaNo() + "','" + inventoryList.get(i).getTrayCode() + "','" + inventoryList.get(i).getElMkDate() + "')");
                    if (i < inventoryList.size() - 1) {
                        sql.append(",");
                    }else if (i == inventoryList.size() - 1){
                        sql.append(";");
                    }
                }
                statement.execute("delete from wms");
                Thread.sleep(5000);
                int i = statement.executeUpdate(sql.toString());
                if (i != -1) {
                    log.info(new Date() + "同步成功");
                }else{
                    log.info("同步失败，数据SQL为：" + sql);
                }
            }
        }catch (SQLException e){
            log.info("同步异常，原因：" + e.getMessage());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void getConnection() throws SQLException {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://10.198.76.23:3306/wcs?characterEncoding=utf-8", "mysqlwrite", "sPM2wptA38NX8sdrsZEQFg==");
            Statement s = conn.createStatement();
            statement = s;
            connection = conn;
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
    }
}

